﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcContrib.Unity;
using Microsoft.Practices.Unity;
using AspZone.Commerce.Repositories;
using AspZone.Commerce.Service;
using MvcContrib.ExtendedComponentController;

namespace Web
{
    public class GlobalApplication : System.Web.HttpApplication, IUnityContainerAccessor
    {
        private static readonly IUnityContainer unityContainer;

        static GlobalApplication()
        {
            unityContainer = new UnityContainer();
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "ViewCategory",
                "Category/{seoName}",
                new { controller = "Catalog", action = "Category", seoName = "" }
                );

            routes.MapRoute(
                "ViewProduct",
                "Product/{seoName}",
                new { controller = "Catalog", action = "Product" }
                );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

            

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            IControllerFactory controllerFactory = new UnityControllerFactory();
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
            

            unityContainer.RegisterType<ICatalogService, CatalogService>();
            unityContainer.RegisterType<ICategoryRepository, CategoryRepository>();
            unityContainer.RegisterType<IProductRepository, ProductRepository>();

            
        }

        public IUnityContainer Container
        {
            get { return unityContainer; }
        }
    }
}